package com.newhero.app.report.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.security.Key;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.servlet.ServletOutputStream;


public class DESFileUtil {
	public static final String Algorithm = "DESede";
	private static String KEYPATH = "";
	
    public static Key getKey() {
        Key kp = null;
        if(KEYPATH == null || "".equals(KEYPATH))
        {
        	//KEYPATH =SysConfigUtil.getConfigItemValue("ROOT.PATH", "D:/");
        	KEYPATH ="D:/workspacexh/report/WebContent/";
        	KEYPATH = KEYPATH.replace("\\", "/");
        	if(!KEYPATH.endsWith("/"))
        	{
        		KEYPATH += "/";
        	}
        	KEYPATH += "WEB-INF/config/DesKey.dat";
        }
        try {
            InputStream is = new FileInputStream(KEYPATH);;
            ObjectInputStream oos = new ObjectInputStream(is);
            kp = (Key) oos.readObject();
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return kp;
    }

    public static void encrypt(String file) throws Exception {
    	Cipher cipher = Cipher.getInstance(Algorithm);
    	cipher.init(Cipher.ENCRYPT_MODE, getKey());
    	InputStream is = new FileInputStream(file);
    	String destFile = file +=".sec";
    	OutputStream out = new FileOutputStream(destFile);
    	CipherInputStream cis = new CipherInputStream(is, cipher);
    	byte[] buffer = new byte[1024];
    	int r;
    	while ((r = cis.read(buffer)) > 0) {
    		//System.out.println("第"+(i++)+"次处理....");
    		out.write(buffer, 0, r);
    	}
    	cis.close();
    	is.close();
    	out.close();
    }
    
    public static void decrypt(String file, String dest) throws Exception {
        Cipher cipher = Cipher.getInstance(Algorithm);
        cipher.init(Cipher.DECRYPT_MODE, getKey());
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(dest);
        CipherOutputStream cos = new CipherOutputStream(out, cipher);
        byte[] buffer = new byte[1024];
        int r;
        int i=0;
        while ((r = is.read(buffer)) >= 0) {
            //System.out.println("第"+(i++)+"次处理....");
            cos.write(buffer, 0, r);
        }
        cos.close();
        out.close();
        is.close();
    }
    
    public static void decrypt(String file, ServletOutputStream out) throws Exception {
    	Cipher cipher = Cipher.getInstance(Algorithm);
    	cipher.init(Cipher.DECRYPT_MODE, getKey());
    	InputStream is = new FileInputStream(file);
    	CipherOutputStream cos = new CipherOutputStream(out, cipher);
    	byte[] buffer = new byte[1024];
    	int r;
    	while ((r = is.read(buffer)) >= 0) {
    		//System.out.println("第"+(i++)+"次处理....");
    		cos.write(buffer, 0, r);
    	}
    	cos.close();
    	is.close();
    }
    //加密
    public static String encryptStr(String str) throws Exception {
    	if(str.trim() == null || str.trim().equals("")){
    		return "";
    	}
    	Cipher cipher = Cipher.getInstance(Algorithm);
    	cipher.init(Cipher.ENCRYPT_MODE, getKey());
    	byte[] dStr = cipher.doFinal(str.getBytes());
    	return parseByte2HexStr(dStr);
    }
    //解密
    public static String decryptStr(String str) throws Exception {
    	if(str.trim() == null || str.trim().equals("")){
    		return "";
    	}
    	Cipher cipher = Cipher.getInstance(Algorithm);
    	cipher.init(Cipher.DECRYPT_MODE, getKey());
    	byte[] dStr = cipher.doFinal(parseHexStr2Byte(str));
    	return new String(dStr);
    }
    
    /**
     * 将二进制转换成16进制
     * 
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     * 
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }


    
    public static void main(String[] args) throws Exception 
    {
        //createDesKey();
        //System.out.println("OK001");
        //long begin = System.currentTimeMillis();
        //encrypt("D:\\stampFile.ftl", "D:\\stampFile1.ftl");
        //decrypt("D:\\stampFile1.ftl", "D:\\stampFile2.ftl");
        //long end = System.currentTimeMillis();
        //System.out.println(" time:" + ((end - begin) / 1000) + "s");
    	//t.createDesKey();
    	//String dd = DESFileUtil.encryptStr("771ED9103325C869F6C0A8685372B0D67A1C64F6EB439109");
    	//System.out.println("dd="+dd.length()+"ddddddd"+dd);
    	System.out.println(DESFileUtil.decryptStr("0E0B7AB682818E07E537F6CDE3FEBE17B4ADAEAF9F65F704"));
    
    	//String d= "07 27 2016  5:03PM";
    	//DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh点mm分ss秒");  
    	//String str = df.format(d);  
    	//System.out.println(str); 
    	SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy  KK:mmaa",Locale.ENGLISH);
		Date d2 = null;
		d2 = sdf.parse("03 17 2016  1:00PM");
		Date d3 = new Date();
		DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh点mm分ss秒");
		String str = df.format(d3);
		//System.out.println("d2 ====== " + d2);
		System.out.println("d3 ====== " + sdf.format(d3));
		System.out.println("str ====== " + str);

      }

}
